import socket
import base64
import time
import threading
import pickle
import optparse
import Skype4Py
from clskypetunnellib import *

parser = optparse.OptionParser(version="0.0.0.xxx")

parser.add_option('-p', '--from-port', type='int', metavar='PORT', help='bind to local PORT')
parser.add_option('-u', '--to-user', metavar='USER', help='redirect data from local PORT to Skype USER; append ":CHANNEL" to the USER to redirect to channel other than 0')
parser.add_option('-a', '--to-addr', metavar='ADDR', help='redirect data from local CHANNEL to ADDR which must be in HOST:PORT format')
parser.add_option('-d', '--udp', action='store_true', help='change the type of the tunnel from to UDP')
parser.add_option('-t', '--tcp', action='store_true', help='change the type of the tunnel from to TCP')
  
opts, args = parser.parse_args()

if args:
    parser.error('unexpected argument(s)')
if opts.from_port != None and opts.to_user != None and opts.to_addr == None:
    mode = 'client'
    addr = '127.0.0.1', opts.from_port
    a = opts.to_user.split(':')
    user = a[0]
    print "It is client: ", addr, user
elif opts.from_port == None and opts.to_user== None and opts.to_addr != None:
    mode = 'server'
    a = opts.to_addr.split(':')
    if len(a) != 2:
        parser.error('bad value of --to-addr')
    addr = a[0], int(a[1])
else:
    parser.error('incorrect argument list')

if opts.tcp:
    stype = socket.SOCK_STREAM
elif opts.udp:
    stype = socket.SOCK_DGRAM
else:
    stype = socket.SOCK_DGRAM

clst = CLSkypeTunnel()
if mode == 'server':
    clst.start_server(addr)
elif mode == 'client':
    clst.start_client(stype, addr, user)
                









